<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">用户信息管理</div>
        <!-- <a-space :size="16">
          <a-button type="primary" @click="importFile">数据导出</a-button>
        </a-space> -->
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="postAppUserPage"  -->
        <BasicTable ref="tableRef" :columns="columns" :query-params="queryData" :apiFetch="postAppUserPage">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'avatar'">
              <a-image :height="50" :width="50" :src="record.avatar" />
            </template>
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="toDetail(record)">查看</a>
                <a class="btn" @click="changeStatus(record)" v-if="!record.status">禁用</a>
                <a class="btn" @click="changeStatus(record)" v-else>启用</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="user-list" setup>
import {
  postAppUserPage,
  postClientUserExport,
  postAppUserUpdateStatus,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const dataSource = ref(
  Array.from({ length: 11 }, () => ({
    username: "2121",
  }))
);
const queryOptions = ref([
  {
    name: "google账号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "email",
    labelCol: { width: "120px" },
  },
  {
    name: "昵称",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "nickname",
    labelCol: { width: "120px" },
  },
  {
    name: "注册时间",
    type: "range-picker",
    value: [],
    placeholder: "请选择",
    filed: "rangeDate",
    dateFields: ["startCreateTime", "endCreateTime"],
  },
]);
const columns = [
  {
    title: "User ID",
    dataIndex: "index",
    width: "150px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "Google 账号",
    dataIndex: "email",
    key: "email",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "昵称",
    dataIndex: "nickname",
    key: "nickname",
    width: "200px",
    ellipsis: true,
  },

  {
    title: "注册时间",
    dataIndex: "loginTime",
    key: "loginTime",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => `${record.status ? "已禁用" : "已启用"}`,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const toDetail = (record) => {
  router.push({
    path: "/user/detail",
    query: {
      id: record?.id,
    },
  });
};

const changeStatus = (record) => {
  const title = `${record.status == 0 ? "启用" : "停用"}`;
  comfirmModal({
    content: `确认${title}该用户账号吗？`,
    onOk() {
      postAppUserUpdateStatus({
        id: record.id,
        status: record.status == 0 ? 1 : 0,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};
// const remove = (id) => {
//   comfirmModal({
//     content: "确认删除该账号",
//     onOk() {
//       postUserDelete({ id }).then(() => {
//         message.success("删除成功");
//         tableRef.value.fetch();
//       });
//     },
//   });
// };

const importFile = () => {
  if (!tableRef.value.data?.length) {
    message.error("暂无数据");
    return;
  }
  const params = {};

  for (let key in queryData) {
    params[key] = queryData[key];
  }
  postClientUserExport(params).then((res) => {
    message.success("导出成功");
  });
};
</script>

<style lang="less" scoped></style>
